**********************************************************************************************************
********************** 
// Appendix Figure 1
do "$ados/FHBoundsCode.ado"
clear
save "$dir/ME_FHdounds.dta", replace emptyok
set seed 67296786
local reps = 1000		
	// Algorithm
	forvalues me = 0.1(0.1)1 {
		
		// ME in all
		use "$dir\DataSCLB", clear
		gen Y = EL_EGRA_PCA_Index 
		replace Y = Y + rnormal(0,`me')
		fhbounds Y, r(`reps') treatment(MT_Program CCT_Program) control(Control) strata(Group_blind) cluster(School_blind) bias
		keep if p==102
		gen me = `me'
		gen version = "All"
		tempfile append
		save `append'
		preserve 
			use "$dir/ME_FHdounds.dta", clear
			append using `append'
			save "$dir/ME_FHdounds.dta", replace
		restore
		
		// ME in Treatments
		use "$dir\DataSCLB", clear
		gen Y = EL_EGRA_PCA_Index 
		replace Y = Y + rnormal(0,`me') if Study_Arm!=0
		fhbounds Y, r(`reps') treatment(MT_Program CCT_Program) control(Control) strata(Group_blind) cluster(School_blind) bias
		keep if p==102
		gen me = `me'
		gen version = "Only T"
		tempfile append
		save `append'
		preserve 
			use "$dir/ME_FHdounds.dta", clear
			append using `append'
			save "$dir/ME_FHdounds.dta", replace
		restore

		// ME in Control
		use "$dir\DataSCLB", clear
		gen Y = EL_EGRA_PCA_Index 
		replace Y = Y + rnormal(0,`me') if Study_Arm==0
		fhbounds Y, r(`reps') treatment(MT_Program CCT_Program) control(Control) strata(Group_blind) cluster(School_blind) bias
		keep if p==102
		gen me = `me'
		gen version = "Only C"
		tempfile append
		save `append'
		preserve 
			use "$dir/ME_FHdounds.dta", clear
			append using `append'
			save "$dir/ME_FHdounds.dta", replace
		restore
	}
		
		use "$dir/ME_FHdounds.dta", clear
		keep *st me version i
		foreach var in FH_up_MT_Program_pt_st FH_lo_MT_Program_pt_st FH_up_CCT_Program_pt_st FH_lo_CCT_Program_pt_st {
				replace `var' = subinstr(`var',"[","",.)
				replace `var' = subinstr(`var',"]","",.)
				split `var' if i==1, parse(",")
				destring `var'*, replace force			
		}
		collapse FH_up_MT_Program_pt_st* FH_lo_MT_Program_pt_st* FH_up_CCT_Program_pt_st* FH_lo_CCT_Program_pt_st*, by(me version)
		
		// Graphs
		# delimit ;
		twoway (line FH_up_MT_Program_pt_st me if version=="All", lp(solid) lw(0.9)) 
			   (line FH_lo_MT_Program_pt_st me if version=="All", lp(dash) lw(0.9)),
			   graphr(c(white))
			   xtitle("SD of Normally Distributed Additional Error Term")
			   legend(label (1 "Rank Preservation") label(2 "Rank Inversion")) 
			   ylabel(0(0.5)3.5,angle(horizontal))
			   ytitle("") subtitle("Bounds on Impact Standard Deviation", justification(left) margin(b t-1|-1) bexpand);
		# delimit cr
		graph export "$output/AppendixFigure1_1.pdf", replace
		
		# delimit ;
		twoway (line FH_up_MT_Program_pt_st me if version=="Only T", lp(solid) lw(0.9)) 
			   (line FH_lo_MT_Program_pt_st me if version=="Only T", lp(dash) lw(0.9)),
			   graphr(c(white))
			   xtitle("SD of Normally Distributed Additional Error Term")
			   legend(label (1 "Rank Preservation") label(2 "Rank Inversion")) 
			   ylabel(0(0.5)3.5,angle(horizontal))
			   ytitle("") subtitle("Bounds on Impact Standard Deviation", justification(left) margin(b t-1|-1) bexpand);
		# delimit cr
		graph export "$output/AppendixFigure1_2.pdf", replace
		
		# delimit ;
		twoway (line FH_up_MT_Program_pt_st me if version=="Only C", lp(solid) lw(0.9)) 
			   (line FH_lo_MT_Program_pt_st me if version=="Only C", lp(dash) lw(0.9)),
			   graphr(c(white))
			   xtitle("SD of Normally Distributed Additional Error Term")
			   legend(label (1 "Rank Preservation") label(2 "Rank Inversion")) 
			   ylabel(0(0.5)3.5,angle(horizontal))
			   ytitle("") subtitle("Bounds on Impact Standard Deviation", justification(left) margin(b t-1|-1) bexpand);
		# delimit cr
		graph export "$output/AppendixFigure1_3.pdf", replace
		
		
